/*
 * Copyright 2012 Finbudget.com
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
package finbudgettable.client.ui.grid.cell.attachment;

import com.google.gwt.cell.client.Cell;
import com.google.gwt.cell.client.FieldUpdater;
import finbudget.client.ui.table.Record;
import finbudget.client.ui.widget.upload.Attachment;
import finbudget.client.ui.widget.upload.UploadWidget;
import finbudgettable.client.ui.grid.CellGrid;
import finbudgettable.client.ui.grid.cell.CommonComparator;
import finbudgettable.client.ui.grid.cell.string.StringFilterPanel;
import finbudgettable.client.ui.grid.column.ColumnMenu;
import finbudgettable.client.ui.grid.column.ColumnMenuImpl;
import finbudgettable.client.ui.grid.column.GridColumn;

public class AttachmentColumn extends GridColumn<UploadWidget.UploadWidgetParams> {

    public AttachmentColumn(Builder builder) {
        super(builder);
        initFieldUpdater(builder.getFieldName());
    }

    @Override
    protected boolean isValueChanged(Record record, UploadWidget.UploadWidgetParams value) {
        return CommonComparator.isNotEqual(record.getValueString(getFieldName()), getValueAsString(value));
    }

    @Override
    protected String getCellStyleNames() {
        return AttachmentCell.RESOURCES.style().bAttachmentCell();
    }

    private String getValueAsString(UploadWidget.UploadWidgetParams params) {
        return params.value;
    }

    private UploadWidget.UploadWidgetParams getValueFromString(String key, String value) {
        UploadWidget.UploadWidgetParams params = new UploadWidget.UploadWidgetParams();
        params.keyNode = key;
        params.value = value;
        return params;
    }

    private void initFieldUpdater(final String fieldName) {
        setFieldUpdater(new FieldUpdater<Record, UploadWidget.UploadWidgetParams>() {
            public void update(int index, Record record, UploadWidget.UploadWidgetParams value) {
                record.setValue(fieldName, getValueAsString(value));
            }
        });
    }

    @Override
    public UploadWidget.UploadWidgetParams getValue(Record record) {
        return getValueFromString(record.getValueString(Attachment.keyNodeId), record.getValueString(getFieldName()));
    }

    public static class Builder extends GridColumn.Builder<AttachmentCell, UploadWidget.UploadWidgetParams> {

        private final UploadWidget.UploadWidgetFactory uploadWidgetFactory;

        Builder(CellGrid grid, String fieldName, UploadWidget.UploadWidgetFactory uploadWidgetFactory, boolean hasTotals) {
            super(grid, fieldName, hasTotals);
            this.uploadWidgetFactory = uploadWidgetFactory;
        }

        @Override
        protected AttachmentCell createCell() {
            return new AttachmentCell(uploadWidgetFactory);
        }

        public ColumnMenu getDefaultColumnMenu() {
            final ColumnMenu columnMenu = new ColumnMenuImpl(grid, new StringFilterPanel(fieldName));
            setColumnMenu(columnMenu);
            return columnMenu;
        }

        @Override
        protected GridColumn<UploadWidget.UploadWidgetParams> createColumn(Cell<UploadWidget.UploadWidgetParams> cell) {
            return new AttachmentColumn(this);
        }
    }

    public static Builder builder(CellGrid grid, String fieldName, UploadWidget.UploadWidgetFactory uploadWidgetFactory, boolean hasTotals) {
        return new Builder(grid, fieldName, uploadWidgetFactory, hasTotals);
    }

}
